package com.foxzzz.flinktest.util;

/**
 * @author: [wujie]
 * @createTime: [2019/12/18 18:04]
 * @description:
 */
public class PredictCollection {
    public static WordMap femaleWordTime = new WordMap();
    public static WordMap maleWordTime = new WordMap();
    public static Integer socre = 0;
    public static Integer sum = 0;
    public static Double baseF = 0D;
    public static Double baseM = 0D;

    public static void incr(String name, String tag) {
        double perLog = 0;
        baseF = baseF < 0 ? 0 : baseF;
        baseM = baseM < 0 ? 0 : baseM;
        if ("0".equals(tag)) {
            double delta = femaleWordTime.incr(name);
            if (sum != 0 && socre != sum) {
                perLog = Math.log(1 - socre / (sum*1.0));
            }
            if (baseF != 0) {
                baseF -= perLog;
            }
            baseF += delta;
            baseF += Math.log(1 - socre / (sum + 1.0));

        } else {
            double delta = maleWordTime.incr(name);
            if (sum != 0) {
                perLog = Math.log(socre / (sum*1.0));
            }
            if (baseF != 0) {
                baseF -= perLog;
            }
            baseM += delta;
            baseM += Math.log(socre / (sum + 1.0));
            socre++;
        }
        sum++;
    }

    /**
     *
     */

    public static PredictVO ComputeLogProb(String name) {
        double m = baseM;
        double f = baseF;
        String[] split = name.split("");
        for (String s : split) {
            m += maleWordTime.GetLogProb(s);
            f += femaleWordTime.GetLogProb(s);
        }

        return new PredictVO(m, f);
    }
}